package com.sunhao.application;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity5 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        DatePicker date_picker = findViewById(R.id.date_picker);
        TextView tv_result = findViewById(R.id.tv_result);


//        日期组件
        Button btn_confirm = findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从日期选择器中拿到日期
                String desc = String.format("您选择的日期是%d年%d月%d日", date_picker.getYear(), date_picker.getMonth() + 1, date_picker.getDayOfMonth());
                tv_result.setText(desc);
            }
        });


        //弹窗日期
        Button select_date = findViewById(R.id.select_date);
        select_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开弹窗，返回选择数据
                DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity5.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        //从日期选择器中拿到日期
                        String desc = String.format("您选择的日期是%d年%d月%d日", year, month + 1, dayOfMonth);
                        tv_result.setText(desc);
                    }
                }, 2090, 5, 20);
                datePickerDialog.show();
            }
        });
    }
}